<template>
	<div class="padding20">
		<el-row style="margin-bottom: 7px">
			<el-form :inline="true" :model="queryForm" @submit.native.prevent @keyup.enter.native="queryList">
				<el-form-item>
					<el-input
						prefix-icon="el-icon-search"
						v-model="queryForm.title"
						autocomplete="off"
						placeholder="请输入应用标题"
						clearable
					></el-input>
				</el-form-item>
				<el-form-item>
					<el-input
						prefix-icon="el-icon-search"
						v-model="queryForm.type"
						autocomplete="off"
						placeholder="请输入应用标题"
						clearable
					></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" size="medium" icon="el-icon-search" @click="queryList({ current: 1 })">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" size="medium" icon="el-icon-refresh-left" @click="resetQueryForm">重置</el-button>
				</el-form-item>
			</el-form>
			<div style="float: right; padding-right: 17px">
				<i class="el-icon-refresh" style="margin-right: 10px; cursor: pointer" @click="refresh"></i>

				<el-popover placement="bottom" width="180" trigger="click" title="列展示" popper-class="columnShows">
					<el-checkbox-group v-model="tableShowList">
						<el-checkbox v-for="item in tableList" :label="item.propName" :key="item.propName" :value="item.propName">
							{{ item.propDesc }}
						</el-checkbox>
					</el-checkbox-group>
					<i slot="reference" class="el-icon-s-operation" style="cursor: pointer"></i>
				</el-popover>
			</div>
		</el-row>
		<el-table v-loading="loading" ref="multipleTable" :data="tableData" style="width: 100%; margin-bottom: 20px" row-key="id" border>
			<el-table-column type="selection" width="55" show-overflow-tooltip></el-table-column>

			<el-table-column prop="appName" label="流程" :show-overflow-tooltip="true"></el-table-column>
			<el-table-column prop="createTime" label="申请时间" :show-overflow-tooltip="true"></el-table-column>
			<el-table-column prop="status" label="状态" :show-overflow-tooltip="true" :formatter="statusFmt"></el-table-column>
			<el-table-column label="操作" fixed="right" min-width="180">
				<template slot-scope="scope">
					<el-button size="mini" plain @click="handleInfo(scope.row)">
						查看详情
					</el-button>
				</template>
			</el-table-column>
		</el-table>
		<pagination :total="page.total" :current-page="page.current" :page-size="page.size" @refreshData="queryList"></pagination>
		<!-- 新增、编辑 -->
		<form-edit v-if="formEditVisible" ref="formEdit" @refreshData="queryList"></form-edit>
	</div>
</template>
<script>
import { csToMe } from "@/api/approves/approves.js"
import { viewTableList } from "@/api/public.api.js"
import FortressPagination from "@/components/fortress-pagination.vue"
import FormEdit from "./formdata-edit"
import { enumList } from "@/api/public.api.js"
export default {
	data() {
		return {
			tableData: [],
			formEditVisible: false,
			queryForm: {
				title: "",
			},
			page: {
				current: 1,
				size: 10,
				total: 0,
			},
			collapse: {
				icon: "el-icon-arrow-down el-icon--right",
				label: "展开",
				flag: false,
			},
			tableList: [],
			tableShowList: [],
			processStatuslist: [],
			loading: false,
		}
	},
	components: {
		"form-edit": FormEdit,
		pagination: FortressPagination,
	},
	created() {
		this.queryEnumList()
		this.initTableList()
		this.queryList()
	},
	methods: {
		refresh() {
			this.tableData = []
			this.queryList({ current: 1 })
		},
		//查询表格数据
		queryList(page) {
			if (page) {
				this.page.current = page.current ? page.current : this.page.current
				this.page.size = page.size ? page.size : this.page.size
			}
			this.queryForm.current = this.page.current
			this.queryForm.size = this.page.size
			this.loading = true
			csToMe(this.queryForm)
				.then(resp => {
					if (resp.code == 0) {
						this.tableData = resp.data.records
						this.page.total = resp.data.total
						//查询页大于总页数，重新查询
						let cu =
							this.page.total % this.page.size == 0
								? parseInt(this.page.total / this.page.size)
								: parseInt(this.page.total / this.page.size + 1)
						if (cu > 0 && this.page.current > cu) {
							this.queryList({ current: cu })
						}
					} else {
						this.$msg({
							message: resp.msg,
							type: "error",
						})
					}
				})
				.finally(() => {
					this.loading = false
				})
		},
		//查看详情
		handleInfo(row) {
			this.$router.push(
				`/approves/flowable-mytask-detail?engineVersion=${row._castle_process_engine_version}&actKey=${row.actKey}&tid=${row.tid}&id=${row.dataId}&pid=${row.processId}`,
			)
		},

		//重置查询框
		resetQueryForm() {
			this.queryForm = {
				title: "",
				flowId: "",
				status: "",
			}
		},
		//切换查询条件收缩框
		handleCollapseState() {
			this.collapse.flag = !this.collapse.flag
			if (this.collapse.flag) {
				this.collapse.icon = "el-icon-arrow-up el-icon--right"
				this.collapse.label = "收起"
			} else {
				this.collapse.icon = "el-icon-arrow-down el-icon--right"
				this.collapse.label = "展开"
			}
		},
		//视图表格列表初始化
		initTableList() {
			viewTableList("castle_flowable_app").then(resp => {
				if (resp.code == 0) {
					this.tableList = resp.data
					this.tableList.forEach(item => {
						this.tableShowList.push(item.propName)
					})
				}
			})
		},
		queryEnumList() {
			this.processStatuslist = []
			enumList("ApproveStatus", "flowable").then(resp => {
				if (resp.code == 0) {
					this.processStatuslist = resp.data
				}
			})
		},
		//状态格式化返回
		statusFmt(row) {
			let name = ""
			if (this.processStatuslist) {
				this.processStatuslist.forEach(item => {
					if (item.code == row.status) {
						name = item.name
					}
				})
			}
			return name
		},
	},
}
</script>
<style scoped></style>
